package com.mayikt.jwt;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.digest.DigestUtils;

import java.util.Base64;

/**
 * @ClassName Test0007
 * @Author 蚂蚁课堂余胜军 QQ644064779 www.mayikt.com
 * @Version V1.0
 **/
public class Test0007 {
    private static String SIGN_KEY = "mayikt";

    public static void main(String[] args) {
        // header /payload/  签名值
        JSONObject header = new JSONObject();
        header.put("alg", "HS256");

        String headerEncode = Base64.getEncoder().encodeToString(header.toJSONString().getBytes());

        JSONObject payload = new JSONObject();
        payload.put("phone", "1592*****100");

        String payloadStr = payload.toJSONString();
        String payloadEncode = Base64.getEncoder().encodeToString(payloadStr.getBytes());
        String sign = DigestUtils.md5Hex(payloadStr + SIGN_KEY);
        String jwt = headerEncode + "." + payloadEncode + "." + sign;
        System.out.println(jwt);
        String[] split = jwt.split("\\.");
        String payloadDecode = new String(Base64.getDecoder().decode(split[1].getBytes()));
        System.out.println("解密后:" + payloadDecode);
        System.out.println(DigestUtils.md5Hex(payloadDecode + SIGN_KEY+"1").equals(split[2]));
    }
}
